package com.golxen.framework.manager;



import com.golxen.utils.LogUtil;

import java.util.ArrayList;
import java.util.List;

public class ManagerPlugin {

    static ManagerPlugin instance = null;

    static List<Class<? extends TempMgr>> mgrClsList = new ArrayList<>();

    List<TempMgr> mgrs = new ArrayList<>();

    public static void start() throws Exception {
        if (instance != null) {
            return;
        }

        instance = new ManagerPlugin();

        for (Class<? extends TempMgr> class1 : mgrClsList) {

            LogUtil.CONSOLE.info("[ManagerPlugin] init {}", class1.getName());
            TempMgr mgr = class1.newInstance();
            if (!mgr.init()) {
                throw new RuntimeException("[ManagerPlugin] init " + class1.getName() + " fail");
            }
            instance.mgrs.add(mgr);

        }
    }

    public static boolean active() {
        return instance != null;
    }

    public static void registMgr(Class<? extends TempMgr> mgrCls) {
        if (mgrClsList.contains(mgrCls)) {
            LogUtil.CONSOLE.info("[ManagerPlugin] regist mgr repeated ========================== {}", mgrCls.getName());
            return;
        }
        mgrClsList.add(mgrCls);
        LogUtil.CONSOLE.info("[ManagerPlugin] regist mgr {}", mgrCls.getName());
    }

    public static List<TempMgr> getMgrs() {
        return instance.mgrs;
    }

}
